package com.joysuch.wwyt.dynamicdata.processor;

import cn.zlg.common.util.CollectionUtils;
import com.joysuch.wwyt.core.common.util.SpringContextUtils;
import com.joysuch.wwyt.dynamicdata.DynamicParam;
import com.joysuch.wwyt.dynamicdata.entity.BaseDynamicFieldConfig;
import com.joysuch.wwyt.dynamicdata.entity.BatchSaveTuple;
import com.joysuch.wwyt.dynamicdata.service.BaseDynamicDataService;
import com.joysuch.wwyt.dynamicdata.service.BaseDynamicFieldConfigService;

import java.util.List;

public class DynamicDataInsertProcessor extends AbstractDynamicDataProcessor implements IDynamicDataProcessor {

    private final BaseDynamicDataService dynamicDataService;
    private final BaseDynamicFieldConfigService dynamicFieldConfigService;

    public DynamicDataInsertProcessor() {
        this.dynamicDataService = SpringContextUtils.getBean(BaseDynamicDataService.class);
        this.dynamicFieldConfigService = SpringContextUtils.getBean(BaseDynamicFieldConfigService.class);
    }

    @Override
    public Object doProcess(DynamicParam param) {
        String bizType = param.getBizType();
        Object object = param.getTargetData();
        List<BaseDynamicFieldConfig> fieldConfigList = dynamicFieldConfigService.findByBizType(bizType);
        BatchSaveTuple tuple = saveDynamicData(object, fieldConfigList);
        if (CollectionUtils.isNotEmpty(tuple.getDataList())) {
            return dynamicDataService.saveBatch(tuple.getDataList());
        }
        return false;
    }

}
